<?php

namespace Xin\Capsule;

use Xin\Support\Traits\Macroable;

abstract class Service
{

	use Macroable, WithConfig, WithContainer;

	/**
	 * @var static
	 */
	protected static $defaultInstance = null;
	/**
	 * @var array
	 */
	protected $config = [];

	/**
	 * @param array $config
	 */
	public function __construct(array $config)
	{
		$this->config = array_replace_recursive($this->config, $config);
	}

	/**
	 * 获取单例
	 * @return static|null
	 */
	public static function getInstance()
	{
		if (static::$defaultInstance == null) {
			static::$defaultInstance = static::makeInstance();
		}

		return static::$defaultInstance;
	}

	/**
	 * 生成实例
	 * @return static
	 */
	public static function makeInstance()
	{
		throw new \RuntimeException('Interface not implemented!');
	}

}
